<?php
class UnitTestCase_Boss_Request extends UnitTestCase_Boss {

    public function testConstruct() {
    
        $request = new Boss_Request('search terms');
        $this->assertEqual($request->getQuery(), 'search terms');
        $this->assertEqual($request->getVertical(), 'web');
        // format gets set by default
        $this->assertEqual($request->getParams(), array('format' => 'xml'));
    
    }
    
    public function testSetVertical() {
    
        $request = new Boss_Request('search terms', 'images');
        $this->assertEqual($request->getVertical(), 'images');
        $request->setVertical('web');
        $this->assertEqual($request->getVertical(), 'web');
    
    }
    
    public function testSetInvalidVerticalThrowsException() {
    
        $this->expectException(new Boss_Request_Exception("Unknown vertical: foo"));
        $request = new Boss_Request('search terms', 'foo');
    
    }
    
    public function testSetInvalidIsCaseInsensitive() {
    
        $request = new Boss_Request('test', 'wEB');
        $this->assertEqual($request->getVertical(), 'web');
    
    }
    
    public function testSetParams() {
    
        $request = new Boss_Request('search terms', 'web', array('format' => 'xml'));
        $this->assertEqual($request->getParams(), array('format' => 'xml'));
        $request->setParams(array('format' => 'json'));
        $this->assertEqual($request->getParams(), array('format' => 'json'));
    
    }
    
    public function testSetInvalidParamThrowsException() {
    
        $this->expectException(new Boss_Request_Exception("Unknown parameter: foo"));
        $request = new Boss_Request('foo', 'web', array('foo' => 'bar'));
    
    }
    
    public function testSetIndividualParam() {
    
        $request = new Boss_Request('foo', 'web', array('start' => 10));
        $request->setParam('format', 'xml');
        $this->assertEqual($request->getParams(), array('start' => 10, 'format' => 'xml'));
    
    }
    
    public function testSetInvalidIndividualParamThrowsException() {
    
        $this->expectException(new Boss_Request_Exception("Unknown parameter: foo"));
        $request = new Boss_Request('foo');
        $request->setParam('foo', 'bar');
    
    }
    
    public function testSetParamIsCaseInsensitive() {
    
        $request = new Boss_Request('foo', 'web', array('fOrmaT' => 'xml'));
        $request->setParam('sTARt', 1);
        $this->assertEqual($request->getParams(), array('format' => 'xml', 'start' => 1));
    
    }
    
    public function testGetParam() {
    
        $request = new Boss_Request('foo', 'web', array('format' => 'xml'));
        $request->setParam('start', 1);
        $this->assertEqual($request->getParam('format'), 'xml');
        $this->assertEqual($request->getParam('start'), 1);
    
    }

    public function testGetParamIsCaseInsensitive() {
    
        $request = new Boss_Request('foo', 'web', array('fOrmaT' => 'xml'));
        $request->setParam('sTARt', 1);
        $this->assertEqual($request->getParam('FORMAT'), 'xml');
        $this->assertEqual($request->getParam('StArT'), 1);
    
    }
    
    public function testGetInvalidParamThrowsException() {
    
        $this->expectException(new Boss_Request_Exception('Parameter \'foo\' is not available'));
        $request = new Boss_Request('foo', 'web', array('fOrmaT' => 'xml'));
        $request->getParam('foo');
    
    }
    
    public function testGetUrl() {
    
        $request = new Boss_Request('search terms', 'web', array('format' => 'xml'));
        $request->setParam('start', 10);
        $request->setParam('count', 10);
        $this->assertEqual($request->getUri(), '/ysearch/web/v1/search%20terms?format=xml&start=10&count=10');
    
    }

}